/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.gui;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * A specific ListActivity to display an array of String and pick one.
 * As soon as the user picks of the elements, setResult gets called with an
 * intent as paramete that contains two extras: 
 * 'selectedIndex', an integer that points to the index of the selected String 
 * in the passed values-array.
 * 'selectedValue' which points to the selected String 
 * 
 * To get the list displayed you need to add a String-array into starting 
 * Intent as extra with name 'values'.
 * 
 * If you want the activity to display a title, put the desired title as an
 * extra with name 'title' into the starting intent. Be aware, that the title
 * of the window gets read by TTS!
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class StringList extends ListActivity {
    
    private String LOG_TAG = "StringList";
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        String title = extras.getString("title");
        Log.i(LOG_TAG, "Displaying list of elements ("+title+") to pick from");
        
        super.setTitle(title);
        
        String[] items = extras.getStringArray("values");        
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        String item = (String) l.getItemAtPosition(position);
        
        Log.i(LOG_TAG, "Picked item '"+ item +"' at position '"+position+"'!");
        
        Intent i = new Intent();
        i.putExtra("selectedIndex", position);
        i.putExtra("selectedValue", item);
        setResult(Activity.RESULT_OK, i);
        finish();
    }    
}
